<?php
//通过$_FILES 获取文件上传信息
$fileInfo = $_FILES['myfile'];
$maxSize = 2097152;
$allowExt = array('jpeg','jpg','png','gif','wemp');

//判断一下错误号
if($fileInfo['error'] == 0)
{
    if($fileInfo['size']>$maxSize)
    {
        exit('上传文件过大');
    }
    //$ext=strtolower(end(explode('.',$fileInfo['name'])));
    $ext=pathinfo($fileInfo['name'],PATHINFO_EXTENSION);
    if(!in_array($ext,$allowExt))
    {
        exit('非法文件类型');
    }   
    //判断文件上传是否是http post
    if(!is_uploaded_file($fileInfo['tmp_name']))
    {
        exit('文件不是通过http post方式上传的');
    }
    //检测是否为真实图片类型
    if(!getimagesize($fileInfo['tmp_name']))
    {
        exit('不是真正的图片类型');
    }
    //确保文件名唯一,防止覆盖
    $path = 'up';
    $umiName = md5(uniqid(microtime(true),true)).'.'.$ext;
    $destination=$path.'/'.$umiName;
    if(!file_exists($path))
    {
        mkdir($path,0777,true);
        chmod($path,0777);
    }
    if(@move_uploaded_file($fileInfo['tmp_name'],$destination))
    {
        echo '文件上传成功';
    }else{
        echo '文件上传失败';
    }
}else{
    switch($error){
        case 1:
            echo "上传文件超过了PHP配置upload_max_filessize的值";
            break;
        case 2:
            echo "超过了表单max_file_size限制的大小";
            break;
        case 3:
            echo "文件部分被上传";
            break;
        case 4:
            echo "没有选择上传文件";
            break;
        case 6:
            echo "没有找到 临时目录";
            break;
        case 7:
        case 8:
            echo "系统错误";
            break;
    }
}
